<template>
  <div style="height: 88%;">
    <div class="littlebutton">
      <div class="input2">
          <el-input
            size="small"
            placeholder="请输入字典名称"
            suffix-icon="el-icon-search"
            v-model="inputtext"
            @keyup.enter.native="searchs"
          >
          </el-input>
        </div>
      <div>
        <span
        ><button type="primary" @click="dialogVisible=true" class="new color">新增</button></span
      >
      <span> <button class="new" @click="shangyi">上移</button></span>
      <span> <button class="new" @click="xiayi">下移</button></span>
      <span ><button class="bigbutton" type="primary" @click="shuxing">属性维护</button></span>
      </div>
    </div>
    <template>
      <el-table
        :data="tableDatas"
        border
        height="calc(100% - 1px)"
        style="width: 100%"
        :header-cell-style="{background:'#f5f5f5','text-align':'center'}"
        highlight-current-row
        @current-change="handleCurrentChange"
      >
        <el-table-column prop="type" label="字典分类编码" width="250">
        </el-table-column>
        <el-table-column prop="value" label="字典值" width="250">
        </el-table-column>
        <el-table-column prop="name" label="字典名称" width="300">
        </el-table-column>
        <el-table-column prop="cascade" label="是否级联" align="center">
          <template #default="{row}">
            <span>{{row.cascade?'是':'否'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="parentValue" label="级联上级值">
        </el-table-column>
        <el-table-column label="操作"  align="center">
          <template #default="{row}">
            <span @click="putfenlei(row)">
              <i
                class="el-icon-edit"
                style="color: skyblue;"
              ></i
            ></span>
            <span @click="del(row)">
              <i
                class="el-icon-delete"
                style="color: red; margin-left: 30px;"
              ></i
            ></span>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <div>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        >
        <div>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
          >
            <el-form-item label="字典值:" prop="dictValue">
              <el-input v-model="ruleForm.dictValue"></el-input>
            </el-form-item>
            <el-form-item label="字典名称:" prop="dictName">
              <el-input v-model="ruleForm.dictName"></el-input>
            </el-form-item>
            <el-form-item label="是否联级：" prop="cascade" style="margin-left: 10px;">
              <el-switch v-model="ruleForm.cascade"></el-switch>
            </el-form-item>
            <div v-show="ruleForm.cascade">
              <el-form-item label="级联上级值:" prop="parentValue">
                <el-input v-model="ruleForm.parentValue"></el-input>
              </el-form-item>
            </div>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="colose">取 消</el-button>
          <el-button type="primary" @click="sure">确 定</el-button>
        </span>
</el-dialog>
    </div>
  </div>
</template>
<script>
import {
  delzidianapi,
  // getdictionarychild,
  getzidianapi,
  postzidianlist,
  // getzidianxinxi,
  getxiazidianapi,
  putzidianxinxiapi,
  getshangzidianapi
} from '../api/dictionary.js'
export default {

  name: 'DictionaryTableone',
  props: ['tableDatas', 'val'],
  data () {
    return {
      value: '',
      inputtext: '',
      row: '',
      dialogVisible: false,
      ruleForm: {
        cascade: false
      },
      rules: {
        dictValue: [
          { required: true, message: '字典值必填', trigger: 'blur' }
        ],
        dictName: [
          { required: true, message: '字典名称必填', trigger: 'blur' }
        ],
        cascade: [
          { required: true, message: '请选择是否联级', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
  },
  methods: {
    del (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        delzidianapi({ dictId: row.id })
        const ress = await getzidianapi({ sortId: this.val.id })
        this.tableDatas = ress.data.list
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async putfenlei (row) {
      this.dialogVisible = true
      this.$set(this.ruleForm, 'dictValue', row.value)
      this.$set(this.ruleForm, 'dictName', row.name)
      this.$set(this.ruleForm, 'cascade', row.cascade)
      this.$set(this.ruleForm, 'parentValue', row.parentValue)
      this.row = row
    },
    async sure () {
      await this.$refs.ruleForm.validate()
      if (!this.row) {
        await postzidianlist({ ...this.ruleForm, sortId: this.val.id })
      } else {
        await putzidianxinxiapi({ ...this.ruleForm, dictId: this.row.id })
      }
      const ress = await getzidianapi({ sortId: this.val.id })
      this.tableDatas = ress.data.list
      this.dialogVisible = false
      this.$message.success('添加成功')
      this.row = null
    },
    colose () {
      this.dialogVisible = false
      this.ruleForm = {}
      this.row = null
    },
    shuxing () {
      this.$router.push('/attribute')
    },
    handleCurrentChange (value) {
      this.value = value
    },
    async shangyi () {
      if (!this.value) {
        this.$message.warning('请选择上移属性')
      } else {
        if (this.value.id === this.tableDatas[0].id) {
          this.$message.warning('已经是第一项 不可移动')
        } else {
          await getshangzidianapi({ dictId: this.value.id })
          this.$message.success('上移成功')
          const ress = await getzidianapi({ sortId: this.val.id })
          this.tableDatas = ress.data.list
        }
      }
    },
    async xiayi () {
      if (!this.value) {
        this.$message.warning('请选择下移属性')
      } else {
        if (this.value.id === this.tableDatas[this.tableDatas.length - 1].id) {
          this.$message.warning('已经是最后一项不可移动')
        } else {
          await getxiazidianapi({ dictId: this.value.id })
          this.$message.success('下移成功')
          const ress = await getzidianapi({ sortId: this.val.id })
          this.tableDatas = ress.data.list
        }
      }
    },
    searchs () {
      this.tableDatas = this.tableDatas.filter(ele => ele.name.indexOf(this.inputtext) !== -1)
      // this.inputtext = ''
    }
  },
  watch: {
    async inputtext (val) {
      this.tableDatas = this.tableDatas.filter(ele => ele.name.indexOf(this.inputtext) !== -1)
      // this.inputtext = ''
      if (this.inputtext === '') {
        const ress = await getzidianapi({ sortId: this.val.id })
        this.tableDatas = ress.data.list
      }
    }
  }
}
</script>

<style lang="less" scoped>
.bigbutton{
  width: 87px;
  height: 32px;
  // padding: -6px 0 0 -6px;
  border: none;
  background-color: #1890ff;
  border-radius: 3px;
  color: #fff;
}
.color{
  background-color: #1890ff !important;
  color: #fff !important;
}
.new{
  width: 63px;
  height: 32px;
  border-radius: 3px;
  color: #333;
  border: 1px solid #ccc;
  background-color: #fff;
}
.littlebutton span {
  margin-right: 10px;
}
.input2{
  width: 300px;
}
.littlebutton{
  // margin-left: 950px;
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
</style>
